package com.sls.game.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.socket.Head;
import com.sls.core.util.IPAddressResolution;
import com.sls.core.util.PropertiesUtil;
import com.sls.game.dao.DownstreamUserDAO;
import com.sls.game.dao.GoodsInfoManageDAO;
import com.sls.game.dao.GoodsOrderDAO;
import com.sls.game.dao.GoodsOrderLogDAO;
import com.sls.game.dao.GoodsSupplyManageDAO;
import com.sls.game.dao.ProvinceDAO;
import com.sls.game.dao.SpecialPriceSystemDAO;
import com.sls.game.dao.TencentGoodsInfoDAO;
import com.sls.game.dao.nutsnet.ChargeTypeInfoDAO;
import com.sls.game.dao.wogreat.WogreatAccountDAO;
import com.sls.game.dao.wogreat.WogreatAsDAO;
import com.sls.game.dao.wogreat.WogreatRechargeDAO;
import com.sls.game.parser.tencent.ParseTencentConfigFile;
import com.sls.game.pojo.DownstreamUserInfo;
import com.sls.game.pojo.GoodsSupplyManageInfo;
import com.sls.game.pojo.ProvinceInfo;
import com.sls.game.pojo.SLSHttpInterfaceInfo;
import com.sls.game.pojo.TencentGoodsInfo;
import com.sls.game.service.TencentService;
import com.sls.game.supply.deal.TencentTradeDeal;
import com.sls.game.supply.deal.WogreatTradeDeal;

/**
 * 
 * @Description：腾讯Q币充值
 * @author wxg
 * @Date：2013-6-14
 * 
 */
@Service("tencentService")
public class TencentServiceImpl implements TencentService {

	@Resource(name = "tencentGoodsInfoDAO")
	private TencentGoodsInfoDAO tencentGoodsInfoDAO;
	@Resource(name = "goodsOrderDAO")
	private GoodsOrderDAO goodsOrderDAO;
	@Resource(name = "goodsOrderLogDAO")
	private GoodsOrderLogDAO goodsOrderLogDAO;
	@Resource(name = "chargeTypeInfoDAO")
	private ChargeTypeInfoDAO chargeTypeInfoDAO;
	@Resource(name = "downstreamUserDAO")
	private DownstreamUserDAO downstreamUserDAO;
	@Resource(name = "goodsInfoManageDAO")
	private GoodsInfoManageDAO goodsInfoManageDAO;
	@Resource(name = "specialPriceSystemDAO")
	private SpecialPriceSystemDAO specialPriceSystemDAO;
	@Resource(name = "goodsSupplyManageDAO")
	private GoodsSupplyManageDAO goodsSupplyManageDAO;
	@Resource(name = "wogreatAccountDAO")
	private WogreatAccountDAO wogreatAccountDAO;
	@Resource(name = "wogreatRechargeDAO")
	private WogreatRechargeDAO wogreatRechargeDAO;
	@Resource(name = "wogreatAsDAO")
	private WogreatAsDAO wogreatAsDAO;
	@Resource(name = "provinceDAO")
	private ProvinceDAO provinceDAO;

	/**
	 * 腾讯Q币充值，IP优先原则
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param head
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param merchantIP
	 * @param gson
	 * @return
	 */
	private String dealTradePriorityOfIp(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, Head head,
			String orderTime, String paymentOrderNo, String merchantIP,
			Gson gson) {
		String resultBody = null;
		// 读取腾讯配置文件
		TencentGoodsInfo serverInfo = ParseTencentConfigFile
				.readTencentConfigFile();
		// IP所属省份名称
		String provice = IPAddressResolution.parseIPAddressOfIP138(merchantIP);
		boolean falg = false;
		falg = checkProvice(provice, serverInfo);
		// 腾讯地址
		String address = null;
		address = serverInfo.getAddress();
		if (address != null && !("".equals(address))) {
			address = address.trim();
		}
		if (falg) { // 调用腾讯的Q币充值（配置的账号）
			resultBody = judgeOfProvince(provice, serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, gson);
		} else { // 调用沃谷的Q币充值
			resultBody = tradeOfWogreat(info, originalGoodsId, orderId, head,
					orderTime, paymentOrderNo, merchantIP);
		}

		return resultBody;
	}

	/**
	 * 调用沃谷工号
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param head
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param merchantIP
	 * @return
	 */
	private String tradeOfWogreat(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, Head head,
			String orderTime, String paymentOrderNo, String merchantIP) {// 平台商品编号
		String resultBody = null;
		String goodsId = info.getGoodsId();
		String suppliersId = "20121008";
		GoodsSupplyManageInfo gsmInfo = new GoodsSupplyManageInfo();
		gsmInfo.setGoodsId(goodsId);
		gsmInfo.setSuppliersId(suppliersId);
		GoodsSupplyManageInfo goodsSmInfo = goodsSupplyManageDAO
				.queryGoodsSupplyInfo(gsmInfo);
		originalGoodsId = null;
		// 供应商名称
		String supplyName = null;
		// 商品进价
		String goodsCostPrice = null;
		if (goodsSmInfo != null) {
			originalGoodsId = goodsSmInfo.getOriginalGoodsId();
			supplyName = goodsSmInfo.getSupplyName();
			goodsCostPrice = goodsSmInfo.getGoodsCostPrice() + "";
		}
		// 工号
		String merchantId = PropertiesUtil.getValue("wogreat.merchant.id",
				"wogreat.properties");
		/**
		 * 更新订单结果
		 */
		WogreatTradeDeal.updateOrderInfo(orderId, supplyName, goodsCostPrice,
				suppliersId, goodsOrderDAO, head, merchantId);
		resultBody = WogreatTradeDeal.dealTradeResultOfWogreat(info,
				originalGoodsId, orderId, orderTime, paymentOrderNo, head,
				goodsOrderDAO, goodsSupplyManageDAO, downstreamUserDAO,
				wogreatAccountDAO, wogreatRechargeDAO, wogreatAsDAO,
				merchantIP, null, null, null);
		return resultBody;
	}

	/**
	 * 判断最终购买门店Ip所属省份是否是配置文件中提供的
	 * 
	 * @param provice
	 * @return
	 */
	private boolean checkProvice(String provice, TencentGoodsInfo serverInfo) {
		boolean falg = false;
		String proviceName = serverInfo.getProviceName();
		if (proviceName != null && !("".equals(proviceName))) {
			proviceName = proviceName.trim();
			String[] proviceNameArr = proviceName.split(";");
			for (int i = 0; i < proviceNameArr.length; i++) {
				String str = proviceNameArr[i];
				if (str.equals(provice)) {
					falg = true;
					break;
				} else {
					continue;
				}
			}
		}

		return falg;
	}

	/**
	 * 腾讯Q币充值，省域优先原则
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param head
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param merchantIP
	 * @param gson
	 * @return
	 */
	private String dealTradeOfProvinceCode(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, Head head,
			String orderTime, String paymentOrderNo, String merchantIP,
			Gson gson) {
		String resultBody = null;
		// 省域编码
		String provinceCode = info.getProvinceCode();
		String provice = null;
		ProvinceInfo pro = provinceDAO.queryProByProvinceCode(provinceCode);
		if (pro != null) {
			provice = pro.getProvinceName();
		}
		boolean falg = false;
		// 读取腾讯配置文件
		TencentGoodsInfo serverInfo = ParseTencentConfigFile
				.readTencentConfigFile();
		falg = checkProvice(provice, serverInfo);
		// 腾讯地址
		String address = null;
		address = serverInfo.getAddress();
		if (address != null && !("".equals(address))) {
			address = address.trim();
		}
		if (falg) { // 调用腾讯的Q币充值（配置的账号）
			resultBody = judgeOfProvince(provice, serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, gson);
		} else { // 调用沃谷的Q币充值
			resultBody = tradeOfWogreat(info, originalGoodsId, orderId, head,
					orderTime, paymentOrderNo, merchantIP);
		}

		return resultBody;
	}

	/**
	 * 调用不同省份对应的工号
	 * 
	 * @param provice
	 * @param serverInfo
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param head
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param merchantIP
	 * @param address
	 * @param gson
	 * @return
	 */
	private String judgeOfProvince(String provice, TencentGoodsInfo serverInfo,
			SLSHttpInterfaceInfo info, String originalGoodsId, String orderId,
			Head head, String orderTime, String paymentOrderNo,
			String merchantIP, String address, Gson gson) {
		String resultBody = null;
		// 调用腾讯的Q币充值（配置的账号）
		if ("浙江".equals(provice)) {
			resultBody = TencentTradeDeal.callZhejiangAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);

		} else if ("广东".equals(provice)) {
			resultBody = TencentTradeDeal.callGuangdongAccount(serverInfo,
					info, originalGoodsId, orderId, head, orderTime,
					paymentOrderNo, merchantIP, address, tencentGoodsInfoDAO,
					gson, goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);

		} else if ("广西".equals(provice)) {
			resultBody = TencentTradeDeal.callGuangxiAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("云南".equals(provice)) {
			resultBody = TencentTradeDeal.callYunnanAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("山东".equals(provice)) {
			resultBody = TencentTradeDeal.callShandongAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("江苏".equals(provice)) {
			resultBody = TencentTradeDeal.callJiangsuAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("江西".equals(provice)) {
			resultBody = TencentTradeDeal.callJiangxiAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("湖北".equals(provice)) {
			resultBody = TencentTradeDeal.callHubeiAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("四川".equals(provice)) {
			resultBody = TencentTradeDeal.callSichuanAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("河南".equals(provice)) {
			resultBody = TencentTradeDeal.callHenanAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("上海".equals(provice)) {
			resultBody = TencentTradeDeal.callShanghaiAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("湖南".equals(provice)) {
			resultBody = TencentTradeDeal.callHunanAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("安徽".equals(provice)) {
			resultBody = TencentTradeDeal.callAnhuiAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		} else if ("福建".equals(provice)) {
			resultBody = TencentTradeDeal.callFujianAccount(serverInfo, info,
					originalGoodsId, orderId, head, orderTime, paymentOrderNo,
					merchantIP, address, tencentGoodsInfoDAO, gson,
					goodsOrderDAO, goodsOrderLogDAO, chargeTypeInfoDAO,
					downstreamUserDAO, goodsInfoManageDAO,
					specialPriceSystemDAO);
		}

		return resultBody;
	}

	/**
	 * 固定工号
	 * 
	 * @return
	 */
	private String dealTradeOfFixed(String tencentAccount,
			String tencentSecretkey, SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, Head head,
			String orderTime, String paymentOrderNo, String merchantIP,
			Gson gson) {
		// 读取腾讯配置文件
		TencentGoodsInfo serverInfo = ParseTencentConfigFile
				.readTencentConfigFile();
		// 腾讯地址
		String address = null;
		address = serverInfo.getAddress();
		if (address != null && !("".equals(address))) {
			address = address.trim();
		}
		String resultBody = null;
		resultBody = TencentTradeDeal.callFixedAccount(tencentAccount,
				tencentSecretkey, info, originalGoodsId, orderId, head,
				orderTime, paymentOrderNo, merchantIP, address,
				tencentGoodsInfoDAO, gson, goodsOrderDAO, goodsOrderLogDAO,
				chargeTypeInfoDAO, downstreamUserDAO, goodsInfoManageDAO,
				specialPriceSystemDAO);

		return resultBody;
	}

	/**
	 * 腾讯Q币充值，调用腾讯工号
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param head
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param merchantIP
	 * @param gson
	 * @return
	 */
	@Override
	public String saveTradeOfTencent(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, Head head,
			String orderTime, String paymentOrderNo, String merchantIP,
			Gson gson) {
		String resultBody = null;
		String userNo = info.getUserNo();
		DownstreamUserInfo userInfo = null;
		userInfo = downstreamUserDAO.queryTencentAccount(userNo);
		// 腾讯Q币工号
		String tencentAccount = null;
		// 腾讯Q币工号密钥
		String tencentSecretkey = null;
		// 腾讯Q币工号优先级
		String tencentPriority = null;
		if (userInfo != null) {
			tencentAccount = userInfo.getTencentAccount();
			tencentSecretkey = userInfo.getTencentSecretkey();
			tencentPriority = userInfo.getTencentPriority();
		}
		if (tencentAccount != null && !("".equals(tencentAccount))) { // 有匹配的工号
			resultBody = dealTradeOfFixed(tencentAccount, tencentSecretkey,
					info, originalGoodsId, orderId, head, orderTime,
					paymentOrderNo, merchantIP, gson);
		} else { // 没有匹配的工号
			if ("1".equals(tencentPriority)) { // IP优先
				resultBody = dealTradePriorityOfIp(info, originalGoodsId,
						orderId, head, orderTime, paymentOrderNo, merchantIP,
						gson);
			} else if ("2".equals(tencentPriority)) { // 省域优先
				resultBody = dealTradeOfProvinceCode(info, originalGoodsId,
						orderId, head, orderTime, paymentOrderNo, merchantIP,
						gson);
			} else { // 默认根据IP优先
				resultBody = dealTradePriorityOfIp(info, originalGoodsId,
						orderId, head, orderTime, paymentOrderNo, merchantIP,
						gson);
			}
		}
		return resultBody;
	}

}
